#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
'''
@author: yuejl
@application:
@contact: lewyuejian@163.com
@file: compat_format.py
@time: 2021/8/22 0022 19:21
@desc:
'''
from typing import Dict, Any, List

def sort_dict_by_custom_order(raw_dict: Dict, custom_order: List):
    '''
    sorted(d.items(), key=lambda x: x[1]) 中 d.items() 为待排序的对象；
    key=lambda x: x[1] 为对前面的对象中的第二维数据（即value）的值进行排序。 key=lambda 变量：变量[维数] 。
    维数可以按照自己的需要进行设置。

    :param raw_dict:
    :param custom_order:
    :return:
    '''

    def get_index_from_list(lst: List, item: Any):
        try:
            return lst.index(item)
        except ValueError:
            # item is not in lst
            return len(lst) + 1

    return dict(
        sorted(raw_dict.items(), key=lambda i: get_index_from_list(custom_order, i[0]))
    )

def _sort_teststep_dict_by_custom_order(data: Dict) -> Dict:
    custom_order = [
        "case_id",
        "child_title",
        "url",
        "method",
        "headers",
        "tips",

    ]
    return sort_dict_by_custom_order(data, custom_order)

def _sort_testcase_dict_by_custom_order(data: Dict) -> Dict:
    custom_order = [
        "case_id",
        "child_title",
        "url",
        "method",
        "headers",
        "tips",
        "priority",
        "ids",
        "child_info",
        "json",
        "except_code",
        "except_msg",

    ]
    return sort_dict_by_custom_order(data, custom_order)
